﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Clueless
{
    public partial class suggestionMade : Form
    {
        private Player Me;
        protected delegate void WTFDelegate(string character, string room, string weapon, string suggester, CardDeck cardDeck, Player me);
        public event EventHandler<SuggestionMadeEventArgs> SuggestionWasDisproved;

        public suggestionMade()
        {
            InitializeComponent();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        internal void SetSuggestion(string character, string room, string weapon, string suggester, CardDeck cardDeck, Player me)
        {
            if (suspectTextBox.InvokeRequired)
                suspectTextBox.Invoke(new WTFDelegate(SetSuggestion), character, room, weapon, suggester, cardDeck, me);
            else
            {
                Me = me;
                suspectTextBox.Text = character;
                weaponTextBox.Text = weapon;
                currentRoomTextBox.Text = room;

                this.Text = "A Suggestion Has Been Made By " + suggester;

                disproveCards.Items.Clear();
                bool canDisprove = false;
                foreach (Card c in cardDeck.Cards)
                {
                    if (c.Name == character)
                    {
                        canDisprove = true;
                        disproveCards.Items.Add(c.Name);
                    }
                    else if (c.Name == weapon)
                    {
                        canDisprove = true;
                        disproveCards.Items.Add(c.Name);
                    }
                    else if (c.Name == room)
                    {
                        canDisprove = true;
                        disproveCards.Items.Add(c.Name);
                    }
                }

                disproveButton.Enabled = canDisprove;
                passButton.Enabled = !canDisprove;
            }
        }

        private void passButton_Click(object sender, EventArgs e)
        {
            SendDisproveMessage("pass");
            this.Hide();
        }

        private void disproveButton_Click(object sender, EventArgs e)
        {
            if (disproveCards.SelectedItems.Count > 0)
            {
                SendDisproveMessage(disproveCards.Items[disproveCards.SelectedIndex].ToString());
                this.Hide();
            }
            else
            {
                MessageBox.Show("Please select one card!", "Select A Card");
                
            }
        }

        /// <summary>
        /// Start Trun
        /// </summary>
        /// <param name="s"></param>
        private void SendDisproveMessage(String message)
        {
            Console.WriteLine("Send Suggestion made!!");
            SuggestionMadeEventArgs args = new SuggestionMadeEventArgs();
            args.message = message;
            args.suggester = Me.Character.Name;
            OnDisproveMessage(args);
        }
        protected virtual void OnDisproveMessage(SuggestionMadeEventArgs e)
        {
            Console.WriteLine("On Suggestion made!!");
            EventHandler<SuggestionMadeEventArgs> handler = SuggestionWasDisproved;
            if (handler != null)
            {
                handler(this, e);
            }
        }
    }
}
